Skip to main content

Logical Operators

if <condition> and <condition>:
       <if body>
if <condition> or <condition>:
       <if body>
if not(<condition>):
       <if body>

Allows for multiple conditions to be combined at the same time.

Operators:
and :
If all conditions are satisfied, the if body is executed.
or :
If at least one of the conditions is satisfied, the if body is executed.
not :
If the reverse of the condition is satisfied, the if body is executed.
Returns:
True or False.
Return Type:
Boolean

IndexIDSpeciesColorWeightAge
0dog_001dogblack405
1cat_001catgolden1.50.2
2cat_002catblack159
3dog_002dogwhite802
4dog_003dogblack250.5
5ham_001hamsterblack13
6ham_002hamstergolden0.250.2
7cat_003catblack100
def more_descriptive_name(id_str, species, color, weight, age):
return id_str + ': This ' + color + ' ' + species + ' weighs ' + weight + ' lbs and is ' + age + ' years old'

def cat_and_dog_info(pet_id):
id_arr = np.array(pets.get('ID'))
if pet_id not in id_arr:
return 'This pet is not in our record'
pets_info = pets[pets.get('ID') == pet_id]
age = pets_info.get('Age').iloc[0]
weight = pets_info.get('Weight').iloc[0]
species = pets_info.get('Species').iloc[0]
color = pets_info.get('Color').iloc[0]
if (species == 'dog') and (age < 1.5):
return pet_id + ': This is a puppy 🐶'
elif (species == 'cat') and (age < 1):
return pet_id + ': This is a kitten 🐱'
elif (species == 'dog') or (species == 'cat'):
weight = str(weight)
age = str(age)
return more_descriptive_name(pet_id, species, color, weight, age)
cat_and_dog_info('dog_001')

'dog_001: This black dog weighs 40.0 lbs and is 5.0 years old'

cat_and_dog_info('cat_001')

'cat_001: This is a kitten 🐱'

cat_and_dog_info('cat_009')

'This pet is not in our record'